package com.example.water_quality_analysis.controller;

import com.example.water_quality_analysis.common.Result;
import com.example.water_quality_analysis.pojo.model.WaterQuality;
import com.example.water_quality_analysis.pojo.req.getAnalysisReq;
import com.example.water_quality_analysis.pojo.vo.WaterDailyQuality;
import com.example.water_quality_analysis.pojo.vo.WaterDailyQualityYear;
import com.opencsv.exceptions.CsvException;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.text.ParseException;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

@Slf4j
@RestController
@RequestMapping("/s1/data")
@Tag(name = "数据控制器", description = "数据控制器")
public class DataController {
    @Autowired
    private com.example.water_quality_analysis.service.DataService DataService;
    @Operation(summary = "获取文件中的数据", description = "获取文件中的数据")
    @PostMapping("/add/file")
    Object getDataFromFile() throws IOException, CsvException, ParseException {
        DataService.getDataFromFile();
        return Result.success(200, "查询成功", null);
    }

    @Operation(summary = "分析数据库中的每日数据",description = "分析数据库中的每日数据")
    @PostMapping("/analysis/daily")
    Object analysisDailyData(@Valid @RequestBody getAnalysisReq req) {
        WaterDailyQuality waterDailyQuality = DataService.analysisDailyData(req.getCurrentPage(), req.getPageSize());
        return Result.success(200, "查询成功", waterDailyQuality);
    }

    @Operation(summary = "分析数据库中的每月数据",description = "分析数据库中的每月数据")
    @PostMapping("/analysis/monthly")
    Object analysisMonthlyData(@Valid @RequestBody getAnalysisReq req) {
        WaterDailyQuality waterDailyQuality = DataService.analysisMonthlyData(req.getCurrentPage(), req.getPageSize());
        return Result.success(200, "查询成功", waterDailyQuality);
    }

    @Operation(summary = "分析数据库中的每年数据", description = "分析数据库中的每年数据")
    @PostMapping("/analysis/yearly")
    Object analysisYearlyData(@Valid @RequestBody getAnalysisReq req) {
        WaterDailyQualityYear waterDailyQuality = DataService.analysisYearlyData( req.getCurrentPage(), req.getPageSize());
        return Result.success(200, "查询成功", waterDailyQuality);
    }

    @Operation(summary = "按输入的具体到天的时间查询你分析数据库中的数据",description = "分析数据库中的数据")
    @PostMapping("/analysis/day")
    Object analysisDayData(@Valid @RequestBody getAnalysisReq req) {
        DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate data = LocalDate.parse(req.getDay(), fmt);
        WaterQuality waterDailyQuality = DataService.analysisDayData(data);
        return Result.success(200, "查询成功", waterDailyQuality);
    }


    @Operation(summary = "按输入的具体到月的时间查询你分析数据库中的数据",description = "分析数据库中的数据")
    @PostMapping("/analysis/month")
    Object analysisMonthData(@Valid @RequestBody getAnalysisReq req) {
        DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate data = LocalDate.parse(req.getDay(), fmt);
        WaterQuality waterDailyQuality = DataService.analysisMonthData(data);
        return Result.success(200, "查询成功", waterDailyQuality);
    }

    @Operation(summary = "按输入的具体到年的时间查询你分析数据库中的数据",description = "分析数据库中的数据")
    @PostMapping("/analysis/year")
    Object analysisYearData(@Valid @RequestBody getAnalysisReq req) {
        DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate data = LocalDate.parse(req.getDay(), fmt);
        WaterQuality waterDailyQuality = DataService.analysisYearData(data);
        return Result.success(200, "查询成功", waterDailyQuality);
    }
}
